package com.cqan.account.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cqan.account.entity.Department;
import com.cqan.account.entity.User;
import com.cqan.account.service.DepartmentService;
import com.cqan.account.service.UserService;
import com.cqan.common.ViewName;
import com.cqan.controller.BaseController;
import com.cqan.exception.AppException;
import com.cqan.utils.encode.EncodeUtils;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController<User,Long,UserService> {
	

	private DepartmentService departmentService;
	
	@RequestMapping(value="add",method=RequestMethod.POST)
	public  ModelAndView add(HttpServletRequest req, HttpServletResponse resp) throws AppException {
		return null;
	}
	
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public  ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws AppException {
		ModelAndView view = new ModelAndView(ViewName.USER_INDEX);
		List<User> users = entityService.getAll();
		List<Department> departments = departmentService.getAll();
		view.addObject("departments", departments);
		view.addObject("users", users);
		return view;
	}
	
	@ResponseBody
	@SuppressWarnings("null")
	@RequestMapping("/getUserByDid")
	public String getUserByDid(Long did){
		if (did==null||did==0) {
			did=1l;
		}
		Department department = departmentService.get(did);
		List<User> users = department.getUsers(); 
		if (users!=null||users.size()>0) {
			JSONArray array = new JSONArray();
			for (User user : users) {
				JSONObject	obj = new JSONObject();
				obj.put("id", user.getId());
				obj.put("realName",user.getRealName());
				array.add(obj);
			}
			return array.toString();
		}
		return "";
	}
	
	@RequestMapping(value="delete",method=RequestMethod.POST)
	public  ModelAndView delete(HttpServletRequest req, HttpServletResponse resp) throws AppException {
		return null;
	}
	
	@RequestMapping(value="/mpass.htm",method=RequestMethod.POST)
	public  @ResponseBody String mpass(String opass,String npass,String repass) throws AppException {
		String msg = "密码修改成功，下次登陆时请使用新密码登陆！";
		User user = getUserDetails();
		if (user!=null) {
			if (!user.getPassword().equals(EncodeUtils.md5(opass, user.getUsername()))) {
				msg= "原始密码输入错误！";
			}else if (!npass.equals(repass)) {
				msg= "两次密码输入不一致!";
			}else {
				user.setPassword(EncodeUtils.md5(npass, user.getUsername()));
				entityService.add(user);
			}
		}
		JSONObject	obj = new JSONObject();
		obj.put("msg", msg);
		return msg;
	}
	@Override
	@Autowired
	public void setEntityService(UserService userService){
		
		this.entityService =userService;
	}

	@Autowired
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	
	
	
	
}
